//
//  EMMImageView.m
//  EMMApp
//
//  Created by Mac on 16/6/12.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "EMMImageView.h"
#import "VGReturnPartPicViewCell.h"

@interface EMMImageView ()<UIScrollViewDelegate,UIGestureRecognizerDelegate> {
    NSInteger lastScale;
}

@property (nonatomic, strong) NSMutableArray *numArr;

@property (nonatomic, strong) NSString *nameSyting;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation EMMImageView
- (id)initWithFrame:(CGRect)frame title:(NSString *)title items:(NSMutableArray *)items {
    if (self == [super initWithFrame:frame]) {
        _numArr = [[NSMutableArray alloc] init];
        self.numArr = items;
        _nameSyting = title;
        
        [self showUI];
    }
    return self;
}

- (void)showUI {
    lastScale = 0;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    view.backgroundColor = [UIColor clearColor];
    view.userInteractionEnabled = YES;
    [self addSubview:view];
    UIView *upNav = [[UIView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, SafeAreaTopHeight)];
    upNav.backgroundColor = [UIColor whiteColor];
    [view addSubview:upNav];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 20+(44-30)/2, 35, 30);
    [button setImage:[[UIImage imageNamed:@"ico_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(tapClick:) forControlEvents:UIControlEventTouchUpInside];
    [upNav addSubview:button];
    
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 44)];
    _nameLabel.text = [NSString stringWithFormat:@"1/%ld",_numArr.count];
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.textColor = [UIColor whiteColor];
    _nameLabel.font = [UIFont systemFontOfSize:17];
    [upNav addSubview:_nameLabel];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight)];    
    for (int i = 0; i < _numArr.count; i++) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(scrollView.bounds.size.width*i, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
        if ([_nameSyting isEqualToString:@"本地"]) {
            VGReturnGoodsPicDTO *temp = _numArr[i];
            _imageView.image = temp.picImage;
        }else if ([_nameSyting isEqualToString:@"网络"]) {
            [_imageView sd_setImageWithURL:[NSURL URLWithString:_numArr[i]] placeholderImage:nil];
        }
       
        [scrollView addSubview:_imageView];
    }

    scrollView.contentSize = CGSizeMake(self.numArr.count * scrollView.frame.size.width, scrollView.frame.size.height);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.bounces = NO;
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    [view addSubview: scrollView];
   
    if ([_nameSyting isEqualToString:@"本地"]) {
        UIButton *buton = [UIButton buttonWithType:UIButtonTypeSystem];
        buton.frame = CGRectMake(EMM_WIDTH-50, (44-30)/2, 35, 40);
        [buton setTitle:@"删除" forState:UIControlStateNormal];
        [buton addTarget:self action:@selector(butonClick:) forControlEvents:UIControlEventTouchUpInside];
        [buton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [upNav addSubview:buton];
        
    }
}

- (void)tapClick:(UIButton *)tap {
    if (_eMMImageViewBlock) {
        _eMMImageViewBlock(_numArr);
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
    _nameLabel.text = [NSString stringWithFormat:@"%ld/%ld",index+1, self.numArr.count];
    lastScale = index;
}

- (void)butonClick:(UIButton *)btn {
    [_numArr  removeObjectAtIndex:lastScale];
    if (_numArr.count > 0) {
       [self showUI];
    }else {
        if (_eMMImageViewBlock) {
            _eMMImageViewBlock(_numArr);
        }
    }
    
}

@end
